﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using KPIReport.Library;
using KPIReport.Library.DomainObject.Administration.UserManagement;
using KPIReport.Library.Enum;
using KPIReport.Library.Interfaces;

namespace KPIReport.Library.DataAccess.Administration.UserManagement
{
    public partial class recRoleRepo : ISearchable<recRole>
    {
        public List<recRole> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recRole> tt = new List<recRole>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    //StringBuilder sbQuery = new StringBuilder();
                    //sbQuery.AppendFormat(" WITH result_set AS ");
                    //sbQuery.AppendFormat(" ( ");
                    //sbQuery.AppendFormat("    SELECT ");
                    //sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    //sbQuery.AppendFormat("      RoleID, RoleName, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate ");
                    //sbQuery.AppendFormat("    FROM ");
                    //sbQuery.AppendFormat("      [master].recRole ");
                    //sbQuery.AppendFormat("    {0}", parameters);
                    //sbQuery.AppendFormat(" ) ");
                    //sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    //cmd.CommandText = sbQuery.ToString();
                    //cmd.CommandType = CommandType.Text;

                    cmd.CommandText = "SP_recRole_GridPage";;
                    cmd.Parameters.Add("@startRow", SqlDbType.Int).Value = startRow;
                    cmd.Parameters.Add("@endRow", SqlDbType.Int).Value = endRow;
                    cmd.Parameters.Add("@sortName", SqlDbType.VarChar).Value = sortName;
                    cmd.Parameters.Add("@sortOrder", SqlDbType.VarChar).Value = sortOrder;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;
                    cmd.CommandType = CommandType.StoredProcedure;
                   
                    

                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recRole t = new recRole();
                                t.RoleID = dr[recRole.Fields.RoleID] == DBNull.Value ? 0 : int.Parse(dr[recRole.Fields.RoleID].ToString());
                                t.RoleName = dr[recRole.Fields.RoleName] == DBNull.Value ? string.Empty : dr[recRole.Fields.RoleName].ToString();
                                t.CreatedBy = dr[recRole.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recRole.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[recRole.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recRole.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[recRole.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recRole.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[recRole.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recRole.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

       

        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Count:
                            //sbQuery.AppendFormat("SELECT COUNT({0}) FROM [master].recRole {1}", column, parameters);
                            cmd.CommandText = "SP_recRole_GridPage_CountData";
                            break;
                       
                        default:
                            // do nothing 
                            break;
                    }

                    //cmd.CommandText = sbQuery.ToString();
                    //cmd.CommandType = CommandType.Text;

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@column", SqlDbType.VarChar).Value = column;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;

                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public int validasiRoleName(string name, short isInternal)
        {
            int jml = -1;

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select COUNT(RoleName) as jmlRoleName from master.recRole where RoleName = @roleName and IsInternal = @isInternal";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@roleName", SqlDbType.VarChar).Value = name;
                    cmd.Parameters.Add("@IsInternal", SqlDbType.Bit).Value = isInternal;

                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            if (dr.Read())
                            {
                                jml = Convert.ToInt32(dr["jmlRoleName"]);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }

            return jml;
        }
    }

}
